package database;

import java.io.ByteArrayOutputStream;

import worst.shopping.WorstShoppingApplication;

import android.app.Application;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class ProductDAO {
	private SQLiteDatabase _dataDataBase;
	private Bitmap _defaultProductImage;
	
	public ProductDAO(Application application)
	{
		WorstShoppingApplication worstApplication = (WorstShoppingApplication)application;
		_dataDataBase = worstApplication._databaseAdapter.getDataBase();
		_defaultProductImage = WorstShoppingApplication.DEFAULT_PRODUCT_IMAGE;
	}
	
	public Product getProduct(String name)
	{
		Cursor cursor = _dataDataBase.query(
				DataBaseKeys.PRODUCT_TABLE, 
				null, 
				DataBaseKeys.PRODUCT_NAME + " = ?", 
				new String[] {name}, 
				null, 
				null, 
				null);
		
		Product product = null;
		
		if(cursor.moveToFirst())
		{
			byte[] bitmapBlob = cursor.getBlob(cursor.getColumnIndex(DataBaseKeys.PRODUCT_ICON));
			
			Bitmap productIcon = null;
			if(bitmapBlob != null)
			{
				productIcon = BitmapFactory.decodeByteArray(bitmapBlob, 0, bitmapBlob.length);
			}
	
			product = new Product(
					cursor.getString(cursor.getColumnIndex(DataBaseKeys.PRODUCT_NAME)), 
					productIcon,
					cursor.getString(cursor.getColumnIndex(DataBaseKeys.PRODUCT_PRICE)),
					cursor.getString(cursor.getColumnIndex(DataBaseKeys.PRODUCT_UNIT)));
		}
		
		cursor.close();
		
		return product;
	}
	
	public void insertProduct(Product product)
	{
		ContentValues contentValues = new ContentValues();
		contentValues.put(DataBaseKeys.PRODUCT_NAME, product.getName());
		contentValues.put(DataBaseKeys.PRODUCT_PRICE, product.getPrice());
		contentValues.put(DataBaseKeys.PRODUCT_UNIT, product.getUnit());
		
		
		if(!product.getIcon().equals(_defaultProductImage))
		{
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			product.getIcon().compress(Bitmap.CompressFormat.PNG, 100, out);
			contentValues.put(DataBaseKeys.PRODUCT_ICON, out.toByteArray());
		}

		_dataDataBase.insert(DataBaseKeys.PRODUCT_TABLE, null, contentValues);	
	}
	
	public Cursor getAllProductsCursor() {
		return _dataDataBase.query(true, DataBaseKeys.PRODUCT_TABLE, new String[]{ "rowid _id", DataBaseKeys.PRODUCT_NAME, DataBaseKeys.PRODUCT_ICON , DataBaseKeys.PRODUCT_PRICE, DataBaseKeys.PRODUCT_UNIT}, null, null, null, null, DataBaseKeys.PRODUCT_NAME + " ASC", null);
	}

	public void updateProduct(Product oldProduct, Product changedProduct) {
		//update list name in lists table
		ContentValues contentValues = new ContentValues();
		contentValues.put(DataBaseKeys.PRODUCT_NAME, changedProduct.getName());
		
		_dataDataBase.update(DataBaseKeys.DATA_TABLE, contentValues, DataBaseKeys.PRODUCT_NAME+ " = ?", new String[]{oldProduct.getName()});
		
		contentValues.put(DataBaseKeys.PRODUCT_PRICE, changedProduct.getPrice());
		contentValues.put(DataBaseKeys.PRODUCT_UNIT, changedProduct.getUnit());		

		//add icon if it exists and reuse content values object to delete entry from product table
		if(!changedProduct.getIcon().equals(_defaultProductImage))
		{
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			changedProduct.getIcon().compress(Bitmap.CompressFormat.PNG, 100, out);
			contentValues.put(DataBaseKeys.PRODUCT_ICON, out.toByteArray());
		}
		
		_dataDataBase.update(DataBaseKeys.PRODUCT_TABLE, contentValues, DataBaseKeys.PRODUCT_NAME+ " = ?", new String[]{oldProduct.getName()});	
	}

	public void deleteProduct(String productname) {
		_dataDataBase.delete(DataBaseKeys.PRODUCT_TABLE, DataBaseKeys.PRODUCT_NAME+" = ?", new String[]{ productname });
		_dataDataBase.delete(DataBaseKeys.DATA_TABLE, DataBaseKeys.PRODUCT_NAME+" = ?", new String[]{ productname });
	}

	public boolean exists(String productname) {
		Cursor cursor = _dataDataBase.query(DataBaseKeys.PRODUCT_TABLE, new String[]{ DataBaseKeys.PRODUCT_NAME }, DataBaseKeys.PRODUCT_NAME + " = ?", new String[]{ productname }, null, null, null);
		boolean exists = cursor.moveToFirst();
		
		cursor.close();
		
		return exists;
	}
}
